package com.t.bricks.bussiness.db.dao.simplemonitor.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.t.bricks.bussiness.db.dao.simplemonitor.SimpleMonitorDao;
import com.t.bricks.bussiness.model.monitor.MonitorInfo;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.IntegerUtil;
import com.t.bricks.utils.function.file.FileUtil;

@Component
public class SimpleMonitorDaoImpl implements SimpleMonitorDao {
		
	/**
	 * 查询监控程序信息
	 * @return
	 */
	@Override
	public MsgEmity search() {
		MsgEmity me = FileUtil.readFile(String.format("%s/data/db/server.info", System.getProperty("user.dir")));
		if (!me.isSuccess()) {
			return me;
		}
		
		String txt = me.getData();
		if (null == txt || "".equals(txt)) {
			return MsgEmity.err(7001, "没有获取到数据");
		}
		
		List<MonitorInfo> list = new ArrayList<MonitorInfo>();
		String[] lines = txt.trim().split(System.lineSeparator());//分行
		for (String line : lines) {
			if (null == line || "".equals(line.trim())) {
				continue;
			}
			
			String[] array = line.trim().split("\t");
			if (array.length != 5) {
				Log.error("文件格式发现错误,行内分隔符错误");
				return MsgEmity.err(7002, "文件格式发现错误,行内分隔符错误");
			}

			MonitorInfo monitorInfo = new MonitorInfo();
			monitorInfo.setsId(array[0]);
			monitorInfo.setsUrl(array[1]);
			monitorInfo.setiUseWatch(IntegerUtil.strToInt(array[2], 1));
			monitorInfo.setsUser(array[3]);
			monitorInfo.setsPass(array[4]);
			
			list.add(monitorInfo);
		}
		
		if (list.size() < 1) {
			return MsgEmity.err(7004, "文件格式发现错误");
		}
		
		return MsgEmity.success(list, "查找成功");
	}
	
	/**
	 * 根据编号取监控程序信息
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity findById(String sId) {
		if (null == sId || "".equals(sId.trim())) {
			return MsgEmity.err(7001, "编号不能为空");
		}
		
		MsgEmity me = FileUtil.readFile(String.format("%s/data/db/server.info", System.getProperty("user.dir")));
		if (!me.isSuccess()) {
			return me;
		}
		
		String txt = me.getData();
		if (null == txt || "".equals(txt)) {
			return MsgEmity.err(7002, "没有获取到数据");
		}
		
		sId = sId.trim();
		
		MonitorInfo monitorInfo = null;
		String[] lines = txt.trim().split(System.lineSeparator());//分行
		for (String line : lines) {
			if (null == line || "".equals(line.trim())) {
				continue;
			}
			
			String[] array = line.trim().split("\t");
			if (array.length != 3) {
				Log.error("文件格式发现错误,行内分隔符错误");
				return MsgEmity.err(7003, "文件格式发现错误,行内分隔符错误");
			}
			
			if (sId.equals(array[0])) {
				continue;
			}
	
			monitorInfo = new MonitorInfo();
			monitorInfo.setsId(array[0]);
			monitorInfo.setsUrl(array[1]);
			monitorInfo.setiUseWatch(IntegerUtil.strToInt(array[1], 1));
			
			break;
		}
		
		if (null == monitorInfo) {
			return MsgEmity.err(7004, "未发现指定编号的信息");
		}
		
		return MsgEmity.success(monitorInfo, "查找成功");
	}

}
